home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo C v2.0 / CBAR.C < prev    next >
Text File  |  1988-08-29  |  4KB  |  130 lines

  1. /*      CBAR.C
  2.  
  3.         Sample program for interfacing Turbo C with Turbo Prolog
  4.  
  5.         Copyright (c) Borland International 1987,88
  6.         All Rights Reserved.
  7. */
  8.  
  9. #include <dos.h>
  10.  
  11. videodot(int x, int y, int color)
  12. {
  13.   union REGS inr,outr;
  14.  
  15.   inr.h.ah = 12;             /* write pixel */
  16.   inr.h.al = color;
  17.   inr.x.cx = x;
  18.   inr.x.dx = y;
  19.   _int86(16,&inr,&outr); /* call video intr */
  20. }
  21.  
  22. /* Draws a line on the screen from (x1, y1) to (x2, y2) in a selected color */
  23. line(int x1, int y1, int x2, int y2, int color)
  24. {
  25.   int xdelta;  /* The change in x coordinates */
  26.   int ydelta;  /* The change in y coordinates */
  27.   int xstep;   /* The change to make in the x coordinate in each step */
  28.   int ystep;   /* The change to make in the y coordinate in each step */
  29.   int change;  /* The amount that the x or y coordinate has changed */
  30.  
  31.   xdelta = x2 - x1;               /* Calculate the change in x coordinates */
  32.   ydelta = y2 - y1;               /* Calculate the change in y coordinates */
  33.   if (xdelta < 0)
  34.   {                                               /* The line will be drawn from right to left */
  35.     xdelta = -xdelta;
  36.     xstep = -1;
  37.   }
  38.   else                                    /* The line will be drawn from left to right */
  39.     xstep = 1;
  40.   if (ydelta < 0)
  41.   {                                               /* The line will be drawn from bottom to top */
  42.     ydelta = -ydelta;
  43.     ystep = -1;
  44.   }
  45.   else                                    /* The line will be drawn from top to bottom */
  46.     ystep = 1;
  47.   if (xdelta > ydelta)    /* x changes quicker than y */
  48.   {
  49.     change = xdelta >> 1;  /* change set to twice the value of xdelta */
  50.     while (x1 != x2)           /* Draw until the terminating dot is reached */
  51.     {
  52.       videodot(x1, y1, color);        /* Draw a dot on the screen */
  53.       x1 += xstep;                            /* Update x coordinate */
  54.       change += ydelta;                       /* Update change */
  55.       if (change > xdelta)
  56.       {
  57.         y1 += ystep;       /* Update the y coordinate */
  58.         change -= xdelta;  /* Reset change */
  59.       }
  60.     }
  61.   }
  62.   else                                       /* y changes quicker than x */
  63.   {
  64.     change = ydelta >> 1;  /* change set to twice the value of ydelta */
  65.     while (y1 != y2)           /* Draw until the terminating dot is reached */
  66.     {
  67.       videodot(x1, y1, color);        /* Draw a dot on the screen */
  68.       y1 += ystep;                            /* Update y coordinate */
  69.       change += xdelta;                       /* Update change */
  70.       if (change > ydelta)
  71.                    /* If change is large enough to update the x coordinate */
  72.       {
  73.         x1 += xstep;            /* Update the x coordinate */
  74.         change -= ydelta;       /* Reset change */
  75.       }
  76.     }
  77.   }
  78. } /* line */
  79.  
  80. cbar_0(int x1, int y1, int width, int height, int color)
  81. {
  82.   int count;  /* Counter variable used in filling bar */
  83.   int x2, y2, x3, y3, x4, y4;  /* Additional points on the bar */
  84.   int wfactor;    /* The number of pixels to shift the bar to the right */
  85.   int hfactor;    /* The number of pixels to shift the bar up */
  86. /*
  87.  
  88. The x and y values are as follows:
  89.  
  90.         x1 x2   x3 x4
  91.     |  |    |  |
  92.     |  v    |  v
  93.     y2--|>/┌────|──┐
  94.     v/ │    v /│
  95.     y1->╔═══════╗/ │
  96.     ║  │    ║  │
  97.     ║  │    ║  │
  98.     ║  │    ║  │
  99.     ║  │    ║  │
  100.     ║  │    ║  │
  101.     y3---->└────║──┘
  102.     ║ /    ║ /
  103.     y4->╚═══════╝/
  104.  
  105. */
  106.  
  107.   wfactor = width / 5;     /* figure out wfactor and hfactor */
  108.   hfactor = height / 12;
  109.   x2 = x1 + wfactor;       /* compute the location of the points on the bar */
  110.   x3 = x1 + width;
  111.   x4 = x3 + wfactor;
  112.   y2 = y1 - hfactor;
  113.   y3 = y1 + height - hfactor;
  114.   y4 = y1 + height;
  115.   line(x1, y1, x3, y1, 2);        /* draw front of the bar */
  116.   line(x3, y1, x3, y4, 2);
  117.   line(x3, y4, x1, y4, 2);
  118.   line(x1, y4, x1, y1, 2);
  119.   line(x3, y1, x4, y2, 2);        /* draw side of the bar */
  120.   line(x4, y2, x4, y3, 2);
  121.   line(x4, y3, x3, y4, 2);
  122.   line(x1, y1, x2, y2, 2);        /* draw top of the bar */
  123.   line(x2, y2, x4, y2, 2);
  124.   line(x1, y4, x2, y3, 3);
  125.   line(x2, y3, x4, y3, 3);        /* draw back of the bar */
  126.   line(x2, y3, x2, y2, 3);
  127.   for (count = y1 + 1; count < y4; count++)         /* fill in the bar */
  128.     line(x1 + 1, count, x3, count, color);
  129. } /* cbar_0 */
  130.